package com.murong.ai.coder.infrastructure.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.murong.ai.coder.entity.TableEntity;
import com.murong.ai.coder.enums.UuidTypEnum;
import com.murong.ai.coder.infrastructure.repository.po.TableDataPO;
import com.murong.ai.coder.infrastructure.repository.po.TableRecordPO;
import com.murong.ai.coder.infrastructure.util.MrDataUtils;
import com.murong.ai.coder.infrastructure.util.MrStringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

public class DatabaseConvert {
    static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static TableEntity toEntity(TableDataPO po) {
        if (po == null) return null;
        TableEntity entity = new TableEntity();
        entity.setTableNameCamel(po.getTableNameCamel());
        entity.setTableNameSnake(po.getTableNameSnake());
        entity.setTableCommentCn(po.getTableCommentCn());
        entity.setTableCommentEn(po.getTableCommentEn());
        entity.setFields(jsonToFields(po.getFieldsJson()));
        entity.setPrimaryKey(jsonToPrimaryKey(po.getPrimaryKeyJson()));
        entity.setIndexes(jsonToIndexes(po.getIndexesJson()));
        entity.setUpdateBy(entity.getUpdateBy());
        entity.setUpdateTime(entity.getUpdateTime());
        if(StringUtils.isBlank(entity.getUpdateTime())){
            entity.setUpdateTime(MrDataUtils.getCurrentTime());
        }
        return entity;
    }

    public static TableDataPO toPO(TableEntity entity) {
        if (entity == null) return null;
        TableDataPO po = new TableDataPO();
        po.setTableNameCamel(entity.getTableNameCamel());
        po.setTableNameSnake(entity.getTableNameSnake());
        po.setTableCommentCn(entity.getTableCommentCn());
        po.setTableCommentEn(entity.getTableCommentEn());
        po.setFieldsJson(fieldsToJson(entity.getFields()));
        po.setPrimaryKeyJson(primaryKeyToJson(entity.getPrimaryKey()));
        po.setIndexesJson(indexesToJson(entity.getIndexes()));
        po.setUpdateBy(entity.getUpdateBy());
        po.setUpdateTime(entity.getUpdateTime());
        if(StringUtils.isBlank(po.getUpdateTime())){
            po.setUpdateTime(MrDataUtils.getCurrentTime());
        }
        return po;
    }

    public static TableDataPO toPO(TableRecordPO entity) {
        if (entity == null) return null;
        TableDataPO po = new TableDataPO();
        po.setTableNameCamel(entity.getTableNameCamel());
        po.setTableNameSnake(entity.getTableNameSnake());
        po.setTableCommentCn(entity.getTableCommentCn());
        po.setTableCommentEn(entity.getTableCommentEn());
        po.setFieldsJson(entity.getFieldsJson());
        po.setPrimaryKeyJson(entity.getPrimaryKeyJson());
        po.setIndexesJson(entity.getIndexesJson());
        po.setUpdateBy(entity.getUpdateBy());
        po.setUpdateTime(entity.getUpdateTime());
        if(StringUtils.isBlank(po.getUpdateTime())){
            po.setUpdateTime(MrDataUtils.getCurrentTime());
        }
        return po;
    }

    public static TableRecordPO toRecordPO(TableEntity entity) {
        TableDataPO po = toPO(entity);
        if (po == null){
            return null;
        } else {
            TableRecordPO recordPO = new TableRecordPO();
            recordPO.setId(MrStringUtils.generateId(UuidTypEnum.TABLE_REC));
            recordPO.setTableNameCamel(po.getTableNameCamel());
            recordPO.setTableNameSnake(po.getTableNameSnake());
            recordPO.setTableCommentCn(po.getTableCommentCn());
            recordPO.setTableCommentEn(po.getTableCommentEn());
            recordPO.setFieldsJson(po.getFieldsJson());
            recordPO.setPrimaryKeyJson(po.getPrimaryKeyJson());
            recordPO.setIndexesJson(po.getIndexesJson());
            recordPO.setUpdateBy(po.getUpdateBy());
            recordPO.setUpdateTime(po.getUpdateTime());
            if(StringUtils.isBlank(recordPO.getUpdateTime())){
                recordPO.setUpdateTime(MrDataUtils.getCurrentTime());
            }
            return recordPO;
        }
    }


    // JSON与对象转换工具
    private static List<TableEntity.Field> jsonToFields(String json) {
        if (json == null) return Collections.emptyList();
        try {
            return OBJECT_MAPPER.readValue(json, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, TableEntity.Field.class));
        } catch (JsonProcessingException e) {
            return Collections.emptyList();
        }
    }

    private static String fieldsToJson(List<TableEntity.Field> fields) {
        if (fields == null) return null;
        try {
            return OBJECT_MAPPER.writeValueAsString(fields);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    private static TableEntity.Index jsonToPrimaryKey(String json) {
        if (json == null) return null;
        try {
            return OBJECT_MAPPER.readValue(json, TableEntity.Index.class);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    private static String primaryKeyToJson(TableEntity.Index index) {
        if (index == null) return null;
        try {
            return OBJECT_MAPPER.writeValueAsString(index);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    private static List<TableEntity.Index> jsonToIndexes(String json) {
        if (json == null) return Collections.emptyList();
        try {
            return OBJECT_MAPPER.readValue(json, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, TableEntity.Index.class));
        } catch (JsonProcessingException e) {
            return Collections.emptyList();
        }
    }

    private static String indexesToJson(List<TableEntity.Index> indexes) {
        if (indexes == null) return null;
        try {
            return OBJECT_MAPPER.writeValueAsString(indexes);
        } catch (JsonProcessingException e) {
            return null;
        }
    }
}
